import {
  Controller,
  Get,
  Post,
  Body,
  Patch,
  Param,
  Delete,
  Put,
  Query,
  Res
} from '@nestjs/common'
import { SalaryService } from './salary.service'
import { CreateSalaryDto } from './dto/create-salary.dto'
import { UpdateSalaryDto } from './dto/update-salary.dto'
import { NoAuth } from 'src/role/decorators/no-auth.decorator'
import {
  ApiBody,
  ApiOperation,
  ApiParam,
  ApiTags,
  ApiQuery
} from '@nestjs/swagger'
import { FResponse } from 'src/utils/reponse'
import { StaffService } from '../staff/staff.service'
@NoAuth()
@ApiTags('工资')
@Controller('salary')
export class SalaryController {
  constructor(
    private readonly salaryService: SalaryService,
    private readonly staffService: StaffService
  ) {}
  /*
    工资 =午餐补助 + 交通补助 + 基本工资 + 奖金 - 罚款
  */
  @Post()
  @ApiOperation({ summary: '创建工资' })
  @ApiBody({ type: CreateSalaryDto, description: '工资信息' })
  async create(@Body() createSalaryDto: CreateSalaryDto) {
    return FResponse.success(await this.salaryService.create(createSalaryDto))
  }

  @Post('/create')
  @ApiOperation({ summary: '创建所有工资' })
  async createAll() {
    const isCreate = await this.salaryService.findIsCreate()
    if (isCreate) return FResponse.fail(null, '已经创建过了')
    const staff = await this.staffService.findAllId()
    await Promise.all(
      staff.map(item => this.salaryService.create({ staffId: item.id }))
    )
    return FResponse.success('批量创建成功')
  }

  @Post('all')
  @ApiOperation({ summary: '获取所有工资' })
  @ApiBody({
    description: '工资信息'
  })
  async findAll(
    @Body()
    body: {
      page: number
      pageSize: number
      isDelete?: boolean
    }
  ) {
    return FResponse.success(
      await this.salaryService.findAll({
        page: +body.page,
        pageSize: +body.pageSize,
        isDelete: body.isDelete
      })
    )
  }

  // 根据员工Id获取工资
  @Post('staff/:id')
  @ApiOperation({ summary: '获取指定员工的工资' })
  @ApiParam({ name: 'id', description: '员工ID' })
  async findByStaffId(
    @Param('id') id: string,
    @Body()
    body: {
      page: number
      pageSize: number
      isDelete?: boolean
    }
  ) {
    return FResponse.success(
      await this.salaryService.findAll(
        {
          page: +body.page,
          pageSize: +body.pageSize,
          isDelete: body.isDelete
        },
        +id
      )
    )
  }

  // 下载工资表
  @Get('download/:year/:month')
  @ApiOperation({ summary: '下载工资表' })
  async download(
    @Res() res,
    @Param('year') year: string,
    @Param('month') month: string
  ) {
    const buffer = await this.salaryService.download(year, month)
    res.set({
      'Content-Type':
        'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
      'Content-Disposition': 'attachment; filename=data.xlsx',
      'Content-Length': buffer.length.toString()
    })
    res.send(buffer)
  }

  // 获取部门平均工资
  @Get('avg')
  @ApiOperation({ summary: '获取部门平均工资' })
  async findAvg() {
    return FResponse.success(await this.salaryService.findAvg())
  }

  // 获取部门总工资
  @Get('sum')
  @ApiOperation({ summary: '获取部门总工资' })
  async findSum() {
    return FResponse.success(await this.salaryService.findSum())
  }

  // 获取全部工资按月份分组
  @Get('six')
  @ApiOperation({ summary: '获取全部工资按月份分组' })
  async findSix() {
    return FResponse.success(await this.salaryService.findSix())
  }

  // 获取上月工资详情按部门分组
  @Get('last')
  @ApiOperation({ summary: '获取上月工资详情按部门分组' })
  async findLast() {
    return FResponse.success(await this.salaryService.findLast())
  }

  // 获取有工资数据的年份
  @Get('year')
  @ApiOperation({ summary: '获取有工资数据的年份' })
  async findYear() {
    return FResponse.success(await this.salaryService.findYear())
  }

  // 根据年份获取有工资数据的月份
  @Get('month/:year')
  @ApiOperation({ summary: '根据年份获取有工资数据的月份' })
  @ApiParam({ name: 'year', description: '年份' })
  async findMonth(@Param('year') year: string) {
    return FResponse.success(await this.salaryService.findMonth(year))
  }

  // 获取指定年月部门职位的工资详情
  /*
    年 月 部门 职位
    1. 0 0 0 0
      - 无
    !2. 1 0 0 0
      - 显示当年的工资
        - x: 月份 y: 工资
    3. 1 1 0 0
      - 显示当月的工资
        - x: 部门 y: 工资
    !4. 1 0 1 0
      - 显示部门当年的工资
        - x: 月份 y: 工资
    *5. 1 1 1 0
      - 显示部门当月的工资
        - x: 职位 y: 工资
    !6. 1 0 1 1
      - 显示职位当月的工资
        - x: 月份 y: 工资
    *7. 1 1 1 1
      - 显示职位当月的工资
        - x: 职位 y: 工资
    ?8. 0 0 1 0
      - 显示部门全部的工资
        - x: 年份 y: 工资
    ?9. 0 0 1 1
      - 显示职位全部的工资
        - x: 年份 y: 工资
  */
  @Get('echarts')
  @ApiOperation({ summary: '获取指定年月部门职位的工资详情' })
  @ApiQuery({ name: 'year', description: '年份' })
  @ApiQuery({ name: 'month', description: '月份' })
  @ApiQuery({ name: 'departmentId', description: '部门ID' })
  @ApiQuery({ name: 'jobId', description: '职位ID' })
  async findEcharts(
    @Query('year') year: string,
    @Query('month') month: string,
    @Query('departmentId') departmentId: string,
    @Query('jobId') jobId: string
  ) {
    if (!year && !month && !departmentId && !jobId) return FResponse.success([])
    return FResponse.success(
      await this.salaryService.findEcharts(year, month, +departmentId, +jobId)
    )
  }

  @Get(':id')
  @ApiOperation({ summary: '获取单个工资' })
  @ApiParam({ name: 'id', description: '工资ID' })
  async findOne(@Param('id') id: string) {
    return FResponse.success(await this.salaryService.findOne(+id))
  }

  @Patch(':id')
  @ApiOperation({ summary: '更新工资' })
  @ApiParam({ name: 'id', description: '工资ID' })
  @ApiBody({ type: UpdateSalaryDto, description: '工资信息' })
  async update(
    @Param('id') id: string,
    @Body() updateSalaryDto: UpdateSalaryDto
  ) {
    return FResponse.success(
      await this.salaryService.update(+id, updateSalaryDto)
    )
  }

  // 批量删除
  @Delete()
  @ApiOperation({ summary: '批量删除工资' })
  @ApiBody({ type: [Number], description: '工资ID数组' })
  async batchRemove(@Body() ids: number[]) {
    return FResponse.success(await this.salaryService.remove(ids))
  }

  // 批量恢复
  @Put('restore')
  @ApiOperation({ summary: '批量恢复工资' })
  @ApiBody({ type: [Number], description: '工资ID数组' })
  async batchRestore(@Body() ids: number[]) {
    return FResponse.success(await this.salaryService.restore(ids))
  }
}
